﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LIT.Logger.Client;

namespace LIT.Logger.ServerCheckerBL
{
    public class ServerLogProcessor
    {
        private static bool m_busy = false;
        private static IEventSender m_eventSender;
        public static IEventSender EventSender
        {
            get
            {
                if ( m_eventSender == null)
                {
                    m_eventSender = new EventSender();
                    m_eventSender.RequestConfigEvent += eventSender_RequestConfigEvent;
                }
                return m_eventSender;
            }

        }

        public static void ProcessEventLog()
        {
            if (m_busy)
            {
                EventSender.ReportEvent(new Event("Event log checking performance problem", EventType.ERROR,
                    "Event log checking did not finish before another check was scheduled.\r\n" +
                    "You should either set EventLogCheckFrequency to higher value, or set reduce maximum event log size."));
                return;
            }
            m_busy = true;
            IConfiguration config = new Configuration();
            try
            {
                EventSender.SendPreviousEvents();
                Checkers.WindowsEventLog eventLog = new Checkers.WindowsEventLog(EventSender, config);
                eventLog.SendEvents();
            }
            catch (Exception ex)
            {
                EventSender.ReportException(new ExceptionEvent("Error in processing logs", EventType.ERROR, ex));
            }
            m_busy = false;
        }

        public static void Process()
        {
            IConfiguration config = new Configuration();
            try
            {
                Checkers.DiskLimit diskLimit = new Checkers.DiskLimit(EventSender, config);
                diskLimit.CheckDrives();

                Checkers.ServiceAndProcess serviceAndProcess = new Checkers.ServiceAndProcess(EventSender, config);
                serviceAndProcess.CheckProcesses();
                serviceAndProcess.CheckServices();

                Checkers.Certificates certificates = new Checkers.Certificates(EventSender, config);
                certificates.CheckCertStores();

                if ( config.SendIsAlive )
                    EventSender.SendIsAlive();
            }
            catch (Exception ex)
            {
                EventSender.ReportException(new ExceptionEvent("Error in processing logs", EventType.ERROR, ex));
            }
        }

        static RequestConfigResult eventSender_RequestConfigEvent(object sender, EventArgs e)
        {
            LIT.Logger.Client.RequestConfigResult result = new LIT.Logger.Client.RequestConfigResult();
            string fileName = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
            result.AddConfigFile(System.IO.Path.GetFileName(fileName), System.IO.File.ReadAllBytes(fileName));
            return result;
        }

       
    }
}
